package com.example.shardingsphere.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.shardingsphere.entity.Order;
import com.example.shardingsphere.mapper.OrderMapper;
import com.example.shardingsphere.param.OrderParam;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RequestMapping("/order")
@RestController("")
public class OrderController {

    @Resource
    private OrderMapper orderMapper;

    @PostMapping("/save")
    public Order get(@RequestBody OrderParam orderParam) {

        Order order = new Order();
        order.setGoodId(orderParam.getGoodId());
        order.setName(orderParam.getName());
        order.setCreateDate(new Date());

        orderMapper.insert(order);
        return order;
    }

    @GetMapping("/id")
    public Order get(@RequestParam("id") String id) {

        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<Order>().eq(Order::getId, id)
                .ge(Order::getCreateDate, DateTime.parse("2021-09-17").toDate())
                .le(Order::getCreateDate, DateTime.parse("2021-10-18").toDate());
        return orderMapper.selectOne(queryWrapper);
    }
}
